<template>
  <el-dialog title="案件财物" :visible.sync="editFormVisible" @opened="dialogOpen">
    <el-form ref="form" :model="form" label-width="80px" size="small">
      <el-form-item prop="name" label="财物名称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item prop="num" label="财物数量">
        <el-input v-model="form.num"></el-input>
      </el-form-item>
      <el-form-item prop="remark" label="财物描述">
        <el-input v-model="form.remark"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :loading="loading" @click="save">保存</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import casePropertyApi from "@/api/system/caseProperty";

export default {
  name: "casePropertyEditForm",
  props: {
    dept: Object,
    value: Boolean
  },
  data() {
    return {
      loading: false,
      editFormVisible: false,
      form: {
        caseId: "",
        name: "",
        remark: "",
        num: ""
      }
    };
  },
  watch: {
    value(val) {
      this.editFormVisible = val;
    },
    editFormVisible(val) {
      this.$emit("input", val);
    }
  },
  methods: {
    dialogOpen() {
      this.$refs.form.resetFields();
      this.form = this.dept;
    },

    save() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.form.id) {
            casePropertyApi
              .mod({ ...this.form, caseId: this.dept.caseId })
              .then(data => {
                this.loading = false;
                this.editFormVisible = false;
                this.$emit("submit");
              });
          } else {
            casePropertyApi
              .add({ ...this.form, caseId: this.dept.caseId })
              .then(data => {
                this.loading = false;
                this.editFormVisible = false;
                this.$emit("submit");
              });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$refs["form"].resetFields();
      this.editFormVisible = false;
    }
  }
};
</script>

